//获取应用实例
const app = getApp()
var util = require('../../utils/util.js');  

Page({
  /**
   * 页面的初始数据
   */
  data: {
    user_id:"",
    hidden:true,
    position_id:"",
    timeId:0,
    area:0,
    click1:0,
    click4:0,
    /**
     * 自己选中的位置
     * 全部人选中的位置
     */
    list: [],
    allList:[],
    addSeatList:[],
    /**
     * 0 过期
     * 1 可预订 
     * 2 有人 
     * 3 已选择
     * 4 我的预订
     * 5 签到成功
     */
    obj : [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]],

    //楼层信息 
    floor: ['1F','2F','3F','4F','5F','6F'],
    floorIndex: 0,
    //时间段
    time: [
      "8:00",
      "9:00",
      "10:00",
      "11:00",
      "12:00",
      "13:00",
      "14:00",
      "15:00",
      "16:00",
      "17:00",
      "18:00",
      "19:00",
      "20:00",
      "21:00"
    ],
    Time: [
      "8:00-9:00",
      "9:00-10:00",
      "10:00-11:00",
      "11:00-12:00",
      "12:00-13:00",
      "13:00-14:00",
      "14:00-15:00",
      "15:00-16:00",
      "16:00-17:00",
      "17:00-18:00",
      "18:00-19:00",
      "19:00-20:00",
      "20:00-21:00",
    ], 
    Area: [
      "1号位",
      "2号位",
      "3号位",
      "4号位",
      "5号位",
      "6号位",
      "7号位",
      "8号位",
      "9号位",
      "10号位",
      "11号位",
      "12号位",
      "13号位",
      "14号位",
      "15号位",
      "16号位",
    ],
    date: [
        '星期一',
        '星期二',
        '星期三',
        '星期四',
        '星期五',
        '星期六',
        '星期天',
    ],
    /**
     * 日期选中
     */
    datePosition: 0,
    /**
     * 上拖动效果越界
     */
    offsetTop: 0,
    /**
     * 做拖动效果越界
     */
    offsetLeft: 0,
    /**
     * 调整左边的滚动条位置
     */
    left: 0,
    /**
     * 调整上的滚动条位置
     */
    top: 0,
    /**
     * 选座区域最小高度
     */
    min: 100,
    /**
     * 选座区域最大高度
     */
    max: 700,
  },
  /**
   * 生命周期函数--监听页面加载
   * 获取用户名，并设置给user_id,根据用户信息获得用户所有的预约座位信息
   * 
   */
  onLoad: function(options) {
    let username = wx.getStorageSync('username')
    console.log(username)
    let that =this
    this.setData({
      user_id:username
    });
    var day_type=new Date().getDay()-1
    if(day_type == -1){
      day_type = 6
    }
    this.setData({
      datePosition:day_type
    })
     //获取该用户的预约信息
     wx.request({
      url: "http://localhost:9090/api/userInfo/seat/myYue", 
      data :{
        user_id:this.data.user_id
      },
      success:(res)=>{   
        //预约表不为空则进行赋值，否则不赋值
        if(res.data.data.length){
          this.setData({
          list:res.data.data
        })
        //将已经过期的预约去掉
        var day_type=new Date().getDay()-1  
        console.log("day_type",day_type)   
        var hour_type=new Date().getHours()-8
        console.log("hour_type",hour_type)   
        /*for(var b=0;b<that.data.list.length;b++){
          if(that.data.list[b].datePosition<day_type){
            //删除掉的要加入到yuyue_all表，用来查违规以及管理员显示
            db_yuyue_all.add({
              data: {
                user_id:that.data.user_id,
                floor:that.data.list[b].floorIndex,
                datePosition:that.data.list[b].datePosition,
                id:that.data.list[b].id
              }
            })
            //今天之前的删掉
            that.data.list.splice(b, 1)//从列表中删除
            b--;         
          }else{
            var te = that.data.list[b].id.split(",");
            var X = parseInt(te[0])
            if(X<hour_type){
              //将此时刻之前的删掉
              that.data.list.splice(b, 1)//从列表中删除
            }
          }
        }*/
        /*that.setData({
          list:that.data.list
        })*/
        /*wx.cloud.callFunction({
          name: "update_yuyue", 
          data :{
            user_id:that.data.user_id,
            list_info:that.data.list
          },
          success(res){   
            console.log("更新yuyue成功",res.result)
          },
          fail(err){
            console.log("失败",err)
          }
        })*/
      }
        //利用云函数获取位置信息
        wx.request({
          url: "http://localhost:9090/api/userInfo/seat/getStatus", 
          method:'POST',
          data :{
            floorIndex : 0,
            datePosition:that.data.datePosition
          },
          success(res){       
            that.setData({
               allList:res.data.data
            })
            console.log("开始初始化")
            //获取当前楼层和日期的全部预约情况，并显示再页面上
            for(var m=0;m<that.data.allList.length;m++){
              var temp = that.data.allList[m].id.split(",");
              var x = parseInt(temp[0])
              var y = parseInt(temp[1])
              that.data.obj[x][y]=2
           }
           console.log(that.data.list)
           //用户有预约
          if(that.data.list.length>0){

            //显示用户当前楼层当前，将用户自己的预约显示当前页面
            for(var m=0;m<that.data.list.length;m++){
              if(that.data.list[m].datePosition!=that.data.datePosition){
                continue
              }else if(that.data.list[m].floorIndex!=that.data.floorIndex){
                continue
              }
              var temp = that.data.list[m].id.split(",");
              var x = parseInt(temp[0])
              var y = parseInt(temp[1])
              //自己的预定  
              that.data.obj[x][y]=4
            }
             // 更新全局数据
              that.setData({
               obj: that.data.obj
              })
              console.log("数据初始化完")
          }    
         },
          fail(err){
            console.log("失败",err)
          }
        })
      },
      fail(err){
        console.log("失败",err)
      }
    })
    var h = this.data.time.length
    var w = this.data.Area.length
    var offsetH = h * 58 + h * 2
    var offsetW = w * 96 + h * 2
    var currentH = offsetH
     // 设置最高高度
    if (offsetH > this.data.max) {
      // offsetH = this.data.max
      currentH = this.data.max
    }
    this.setData({
      offsetH: offsetH,
      offsetW: offsetW,
      currentH: currentH
    })
  },

  //楼层选择--->对应的座位界面需要刷新
  bindFloorPickerChange: function (e) {
    for(var i=0;i<13;i++){
      for(var j=0;j<16;j++){
        this.data.obj[i][j]=1
      }
    }
    this.setData({
      obj: this.data.obj
     })
    console.log(this.data.datePosition)
    this.data.click1=0
    this.data.click4=0
    if(this.data.click1%2!=0){
      wx.showToast({
        title: '切换楼层前先确定提交',
        icon:"none",
        duration:2000
      })
      return
    }else if(this.data.click4%2!=0){
      wx.showToast({
        title: '切换楼层前先确定提交',
        icon:"none",
        duration:2000
      })
      return
    }
    let that=this
    this.setData({
      floorIndex: e.detail.value,
    })
    wx.request({
      url: "http://localhost:9090/api/userInfo/seat/getStatus", 
      method:'POST',
      data :{
      floorIndex : parseInt(this.data.floorIndex),
      datePosition:parseInt(this.data.datePosition)
      },
      success(res){  
        that.setData({
          allList:res.data.data
        })
        console.log("变换楼层")
        //获取当前楼层和日期的全部预约情况，并显示再页面上
        for(var m=0;m<that.data.allList.length;m++){
          var temp = that.data.allList[m].id.split(",");
          var x = parseInt(temp[0])
          var y = parseInt(temp[1])
          that.data.obj[x][y]=2
        }
        for(var m=0;m<that.data.list.length;m++){
          if(that.data.list[m].datePosition!=that.data.datePosition){
            continue
          }else if(that.data.list[m].floorIndex!=that.data.floorIndex){
            continue
          }
          var temp = that.data.list[m].id.split(",");
          //自己的预定  
          var x = parseInt(temp[0])
          var y = parseInt(temp[1])
          that.data.obj[x][y]=4 
        }
        that.setData({
          obj:that.data.obj
        })
      },
      fail(err){
        console.log("失败",err)
      }
    })
  },
  /**
   * 选择日期--->对应的座位界面需要刷新
   */
  chooseDate: function(e) {
    for(var i=0;i<13;i++){
      for(var j=0;j<16;j++){
        this.data.obj[i][j]=1
      }
    }
    this.setData({
      obj: this.data.obj
     })
    this.data.click1=0
    this.data.click4=0
    var index = parseInt(e.currentTarget.id)
    console.log(index)
    var day_type=new Date().getDay()-1
    if(index<day_type){
      wx.showToast({
        title: '今天之前的不能再预约哦！',
        icon:"none",
        duration:2000
      })
      return
    }
    if(this.data.click1%2!=0){
      wx.showToast({
        title: '切换日期前先确定提交',
        icon:"none",
        duration:2000
      })
      return
    }else if(this.data.click4%2!=0){
      wx.showToast({
        title: '切换日期前先确定提交',
        icon:"none",
        duration:2000
      })
      return
    }
    let that=this
    //this.data.datePosition取值为0，1，2，3，4,5,6,
    if (index != this.data.datePosition) {
      this.setData({
        datePosition: index
      })
      wx.request({
        url: "http://localhost:9090/api/userInfo/seat/getStatus", 
        method:'POST',
        data :{
        floorIndex : parseInt(this.data.floorIndex),
        datePosition:parseInt(this.data.datePosition)
        },
        success(res){
          // console.log("日期选择成功",res.result)
          that.setData({
            allList:res.data.data
          })
          console.log("变换日期")
          //获取当前楼层和日期的全部预约情况，并显示再页面上
          for(var m=0;m<that.data.allList.length;m++){
            var temp = that.data.allList[m].id.split(",");
            var x = parseInt(temp[0])
            var y = parseInt(temp[1])
            that.data.obj[x][y]=2
          }
          for(var m=0;m<that.data.list.length;m++){
            if(that.data.list[m].datePosition!=that.data.datePosition){
              continue
            }else if(that.data.list[m].floorIndex!=that.data.floorIndex){
              continue
            }
            var temp = that.data.list[m].id.split(",");
            var x = parseInt(temp[0])
            var y = parseInt(temp[1])
            //自己的预定  
            that.data.obj[x][y]=4 
          }
          that.setData({
            obj:that.data.obj
          })
        },
        fail(err){
          console.log("失败",err)
        }
      })
    }
  },
  /*
  *移动界面，并显示移动界面
  */
  bindChange: function(e) {
    var x = e.detail.x
    var y = e.detail.y
    var offsetTop = 0;
    if (y > 0) {
      offsetTop += y
    }
    var offsetLeft = 0;
    if (x > 0) {
      offsetLeft += x;
    }
    this.setData({
      left: x,
      offsetLeft: offsetLeft,
      offsetTop: offsetTop,
      top: y
    })
  },
  /*
  *选择座位
  */
  choose: function(e) {
    let that=this
    var p = e.currentTarget.id.split(",")
    //i行号（从0开始）
    //j列号（从0开始）   
    var i = parseInt(p[0])
    var j = parseInt(p[1])
    var id = i + ',' + j // 1,2 
    this.setData({
      position_id:id
    })
    var item = this.data.obj[i][j]
    //    1 可预订 
    //    2 已售完
    //    3 已选择
    //    4 我的预订     
    var status = item
    /**
     * 可预订
     */
    if (status == 1) {
      this.data.click1++
      //判断数组长度最多选4座
      if (this.data.list.length == 4) {
        wx.showToast({
          title: '最多选择4场',
          icon: 'none',
          duration: 2000
        })
        return
      }
      for (var index in this.data.list){
        var temp = this.data.list[index].id.split(",");
        var x = parseInt(temp[0])
        var y = parseInt(temp[1])
        //判断是否为一天
        if(this.data.list[index].datePosition==this.data.datePosition){
        if (x == i) {
          //判断在一行
          wx.showToast({
            title: '同一时段只能选择一个座位',
            icon:'none',
            duration: 2000
          })
          return
        }
      }
      };
      that.data.obj[i][j]=3
      /**
       * 生成框里的数据
       */
      this.data.list.push({       
        floorIndex:this.data.floorIndex,
        datePosition:this.data.datePosition,
        timeId:i,
        area:j,
        id: id,       
      })
      this.data.addSeatList.push({       
        floorIndex:this.data.floorIndex,
        datePosition:this.data.datePosition,
        timeId:i,
        area:j,
        id: id,       
      })
      /**
       * 更新全局数据
       */
      this.setData({
        obj: this.data.obj,
        list: this.data.list,
        addSeatList:this.data.addSeatList
      })
      console.log(this.data.list)
      console.log(this.data.addSeatList)
    } else if (status == 3) {
      /**
       * 已选择 取消选择
       */   
      item = 1 //将其状态恢复
      that.data.obj[i][j]=1
      this.data.click1--
      for (var m = 0; m < this.data.list.length; m++) {
        if (this.data.list[m].id == id) {
          this.data.list.splice(m, 1)//从列表中删除
          break;
        }
      }
      for (var m = 0; m < this.data.addSeatList.length; m++) {
        if (this.data.addSeatList[m].id == id) {
          this.data.addSeatList.splice(m, 1)//从列表中删除
          break;
        }
      }
      /**
       * 全局赋值
       */
      this.setData({
        obj: this.data.obj,
        list: this.data.list,
        addSeatList:this.data.addSeatList
      })
      console.log(this.data.list)
      console.log(this.data.addSeatList)
    }else if(status == 4) {      
      this.setData({
        hidden:false
      })
      this.data.click4++
    }else if (status == 2) {
      wx.showToast({
        title: '已被预定',
        icon: 'none',
        duration: 2000
      })
    } 
  },
   /**
   * 确认预定，修改数据库
   * 1.提交数据到数据库（位置信息表，我的预约表）
   */
  confirm: function(e) { 
    if (this.data.list.length==0) {
      wx.showToast({
        title: '还没有选择哦！',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var day_type=new Date().getDay()-1  
    if(this.data.datePosition<day_type){
      wx.showToast({
        title: '今天之前的不能再预约哦！',
        icon:"none",
        duration:2000
      })
      return
    }
    let that=this
    //给预约表中写入数据或者更新yuyue表
    
    if(this.data.addSeatList){
      wx.request({
        url: 'http://localhost:9090/api/userInfo/seat/addSeat',
        method:"POST",
        data:{
          userId:this.data.user_id,
          seatVOList:this.data.addSeatList
        },
        success:(res)=>{
          //将obj[i][j]中选定的值由3改为2(有人状态)
        for(var a=0;a<this.data.addSeatList.length;a++){
         var temp = this.data.addSeatList[a].id.split(",");
         var x = parseInt(temp[0])
         var y = parseInt(temp[1])
          if(this.data.obj[x][y]==3){
           this.data.obj[x][y]=4;
         }  
         wx.showToast({
          title: '预定成功！',
          icon:"none",
          duration:2000
        })
        }
        this.setData({
          obj: this.data.obj,
          addSeatList:[]
        })
        },
        fail(res){
          wx.showToast({
            title: '预定失败！',
            icon:"none",
            duration:2000
          })          
        }
      })
      }
      /*wx.cloud.callFunction({
            name: "updata_position", 
            data :{
              floorIndex : parseInt(that.data.floorIndex),
              datePosition:parseInt(that.data.datePosition),
              obj:that.data.obj
            },
            success(res){   
              console.log("更新position_info成功",res.result)
            },
            fail(err){
              console.log("失败",err)
            }
          })*/            
          this.setData({      
            click4:0,
            click1:0
          })
          
  },
  /*
  *取消预订，删除数据库
   */
  conf(){
    let confSuccess=0
    wx.request({
      url: 'http://localhost:9090/api/userInfo/seat/deleteSeat',
      method:'POST',
      header:{
        'Content-Type': 'application/json'
      },
      data :{
        userId:this.data.user_id,
        id:this.data.position_id,
        floorIndex : parseInt(this.data.floorIndex),
        datePosition:parseInt(this.data.datePosition)
        },
        success:(res)=>{
          console.log("删除座位预约")
              //i行号（从0开始）
              //j列号（从0开始
              confSuccess=1
        },
        fail(err){
          return
        }
    })  
    if(confSuccess==1){
      return
    }
    var p = this.data.position_id.split(",")
          var i = parseInt(p[0])
          var j = parseInt(p[1])
        for (var index in this.data.list){
        var temp = this.data.list[index].id.split(",");
        var x = parseInt(temp[0])
        var y = parseInt(temp[1])
        if(x==i&&y==j){//删掉对应的预约信息
          this.data.list.splice(index,1)
        }
      };
      for (var index in this.data.addSeatList){
        var temp = this.data.addSeatList[index].id.split(",");
        var x = parseInt(temp[0])
        var y = parseInt(temp[1])
        if(x==i&&y==j){//删掉对应的预约信息
          this.data.addSeatList.splice(index,1)
        }
      };
      //list表有改变
      this.data.obj[i][j]=1
      /**
       * 全局赋值
       */
      this.setData({
        obj: this.data.obj,
        list: this.data.list,
        addSeatList:this.data.addSeatList
      })
      this.setData({
        hidden:true
      })
      wx.showToast({
        title: '取消成功',
        icon: 'success',
        duration: 2000
      })
  },
  cancel(){
    this.setData({
      hidden:true,
      click4:0
    })
  },
//签到
  signIn(e){
    console.log(e.currentTarget.dataset.information)
    let signDate=e.currentTarget.dataset.information
    let todayDate=e.currentTarget.dataset.information.datePosition
    let day=new Date().getDay()-1
    /*if(todayDate!=day){
      wx.showModal({
        title: '提示',
        content: "只能签到今天的日期",
        showCancel:false
      })
      return
    }*/
    var p = signDate.id.split(",")
    console.log(p)
    var i = parseInt(p[0])
    var j = parseInt(p[1])
     if(this.data.obj[i][j]!=4){
      wx.showModal({
        title: '提示',
        content: "只能签到自己已经选择的预订",
        showCancel:false
      })
      return
     }
    wx.showModal({
      title: '提醒',
      content: '是否签到',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '取消签到',
            icon: 'error',
            duration: 2000
          })
        }
        if (res.confirm) {
          
          wx.request({
            url: 'http://localhost:9090/api/userInfo/seatAll/sign',
            method:"POST",
            data:{
              userId:this.data.user_id,
              id:signDate.id,
              floorIndex : parseInt(signDate.floorIndex),
              datePosition:parseInt(signDate.datePosition)
            },
            success:(res)=>{
              console.log(res.data)
              for (var index in this.data.list){
                var temp = this.data.list[index].id.split(",");
                var x = parseInt(temp[0])
                var y = parseInt(temp[1])
                if(x==i&&y==j){//删掉对应的预约信息
                  this.data.list.splice(index,1)
                }
              };
              for (var index in this.data.addSeatList){
                var temp = this.data.addSeatList[index].id.split(",");
                var x = parseInt(temp[0])
                var y = parseInt(temp[1])
                if(x==i&&y==j){//删掉对应的预约信息
                  this.data.addSeatList.splice(index,1)
                }
              };
              //list表有改变
              this.data.obj[i][j]=1
              /**
               * 全局赋值
               */
              this.setData({
                obj: this.data.obj,
                list: this.data.list,
                addSeatList:this.data.addSeatList
              })
              this.setData({
                hidden:true
              })
              wx.showToast({
                title: '签到成功',
                icon: 'success',
                duration: 2000
              })

            },
            fail(res){
                wx.showToast({
                  title: '签到失败',
                  icon: 'fail',
                  duration: 2000
          })   
          return    
            }
          })
                 
        }
      }
    })
  }
})